let report=0;//type 1 周报 2 月报
(function(){
      //日期选择函数
    function GetAnalysisWindowFiterTimes(valuetoid, type, event) {
        var ntime = new Date();
        if (type == 'd') {
              var dstart = ntime.format("MM/dd/yyyy");
              var dend = ntime.format("MM/dd/yyyy");
              $(".timeControl #" + valuetoid).val(dstart + " - " + dend);
        } else if (type == 'w') {
              var wstart = new Date(ntime.getTime() + (24 * 60 * 60 * -6 * 1000)).format("MM/dd/yyyy");
              var wend = ntime.format("MM/dd/yyyy");
              $(".timeControl #" + valuetoid).val(wstart + ' - ' + wend);
        } else if (type == 'm') {
              var mstart = new Date(ntime.getTime() + (24 * 60 * 60 * -30 * 1000)).format("MM/dd/yyyy");
              var mend = ntime.format("MM/dd/yyyy");
              $(".timeControl #" + valuetoid).val(mstart + ' - ' + mend);
        }
        eval(event);
  }
  GetAnalysisWindowFiterTimes('table_time', 'd', false); 
   //选择日期触发事件
   $('.timeControl #table_time').daterangepicker(null, function (start, end, label) {
      times = $(".timeControl #table_time").val();
      clickType(null,"")
      getTableData(times);
   }); 
})();
function clickType(item,val){
      report=val;
      if(item != null){
         $(item).addClass("active").siblings().removeClass("active");
      }else{
         $(".cycle span").removeClass("active");
      }
      getTableData();
}
function getTableData(val){
      let times;
      if(val ==undefined){
            times=$(".timeControl #table_time").val();
      }else{
            times=val;
      }
      var startTimeSplit = times.split('-')[0].replace(" ", "").split('/'),
      endTimeSplit = times.split('-')[1].replace(" ", "").split('/');
      var startTime = startTimeSplit[2] + "-" + startTimeSplit[0] + "-" + startTimeSplit[1];
      var endTime = endTimeSplit[2] + "-" + endTimeSplit[0] + "-" + endTimeSplit[1];
      layui.use(['table','layer','form','layedit','laydate'], function(){
            var table = layui.table;
            var layer=layui.layer;
            var layedit = layui.layedit;
            var form=layui.form;
            var $=layui.$;
            var laydate = layui.laydate;
            table.render({
                  elem: '#idTest',
                  url:'http://192.168.9.59:18902/report/findByValues',
                  contentType: "application/x-www-form-urlencoded",
                  page: { 
                        layout: [ 'prev', 'page', 'next', 'skip','limit', 'count',] //自定义分页布局
                        , curr: 1 //设定初始在第 1 页
                        , limit: 10//每页多少数据
                        , groups: 5 //只显示 5 个连续页码
                        ,limits:[5,10,15]//每页条数的选择项 
                  },
                  where: {startTime:startTime,endTime:endTime,type:report},
                  parseData: function(res){ //res 即为原始返回的数据
                        return {
                                    "code": res.code, //解析接口状态
                                    "msg": res.msg, //解析提示文本
                                    "count": res.total, //解析数据长度
                                    "data": res.data //解析数据列表
                              };
                        },
                  cols: [[
                              ,{field:'time',title: '报告日期', width:150}
                              ,{field:'type', title: '类别',width:150}
                              ,{field:'path', title: '名称',width:300}
                              ,{fixed: 'right', title:'操作',width:120, toolbar: '#barDemo',align:'center'}       
                        ]],
                  done:function(res, curr, count){ //res 接口返回的信息
                        $("[data-field = 'type']").children().each(function(){
                              if($(this).text() == '1'){
                                    $(this).text("周报");
                              }else if($(this).text() == '2'){
                                          $(this).text("月报");
                              }
                        })
                  }   
                          
            });  
            table.on('tool(test)', function(obj){
                  var data = obj.data;
                  if(obj.event === 'down'){
                      //下载
                      window.location.href="http://192.168.9.59:18902/report/expol?id="+data.id;
                  //     $.post("http://192.168.9.59:18902/report/expol",{id:data.id}, function (res) {
                  //     layer.msg(res.msg)
                  //      });  
                  }
            })

      })

}
getTableData();
